package cn.dnui_lf_705.dnui_16110100705;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.dnui_lf_705.dnui_16110100705.Adapter.ShopAdapter;
import cn.dnui_lf_705.dnui_16110100705.Fragment.CollectFragment;
import cn.dnui_lf_705.dnui_16110100705.Fragment.HomeFragment;
import cn.dnui_lf_705.dnui_16110100705.Fragment.MeFragment;
import cn.dnui_lf_705.dnui_16110100705.Fragment.SearchFragment;
import cn.dnui_lf_705.dnui_16110100705.bean.ShopBean;


public class MainActivity extends AppCompatActivity {
    private ImageButton ib1,ib2,ib3,ib4;
    private ViewPager vp1;
    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;
    private RecyclerView recyclerView;
    private List<ShopBean> shopBeans;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        ib1=(ImageButton)findViewById(R.id.id_tab_shouye_img);
//        ib2=(ImageButton)findViewById(R.id.id_tab_collect_img);
//        ib3=(ImageButton)findViewById(R.id.id_tab_search_img);
//        ib4=(ImageButton)findViewById(R.id.id_tab_my_img);
        ib1=(ImageButton)findViewById(R.id.ib1);
        ib2=(ImageButton)findViewById(R.id.ib2);
        ib3=(ImageButton)findViewById(R.id.ib3);
        ib4=(ImageButton)findViewById(R.id.ib4);

//        tv1 = findViewById(R.id.shouye);
//        tv2 = findViewById(R.id.collect);
//        tv3 = findViewById(R.id.search);
//        tv4 = findViewById(R.id.my);
/*        gp1 = findViewById(R.id.group1);
        gp2 = findViewById(R.id.group2);
        gp3 = findViewById(R.id.group3);*/

        vp1=(ViewPager)findViewById(R.id.viewpager1);
        initEvents();
        initFragment();

        ShopAdapter mAdapter = new ShopAdapter(this,listfragment);
        mAdapter.setOItemClickListener(new ShopAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, Object data) {
                ShopBean shopBean = (ShopBean) data;
                Toast.makeText(MainActivity.this,shopBean.getShopname(),Toast.LENGTH_LONG).show();
            }
        });
//        recyclerView = findViewById(R.id.shop_list);
//        recyclerView.setLayoutManager(new LinearLayoutManager(R.id.shop_list));
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
//        shopBeans = getData();
//        recyclerView.setAdapter(new ShopAdapter(this,shopBeans));

    }
    private void resetImage()
    {
        ib1.setBackgroundColor(getResources().getColor(R.color.ivory));
        ib2.setBackgroundColor(getResources().getColor(R.color.ivory));
        ib3.setBackgroundColor(getResources().getColor(R.color.ivory));
        ib4.setBackgroundColor(getResources().getColor(R.color.ivory));
    }

    private void selectTab(int i)
    { switch (i)
    {
//        case 0:ib1.setImageResource(android.R.drawable.btn_star_big_on);
//        break;
//        case 1:ib2.setImageResource(android.R.drawable.btn_star_big_on);
//            break;
//        case 2:ib3.setImageResource(android.R.drawable.btn_star_big_on);
//            break;
//        case 3:ib4.setImageResource(android.R.drawable.btn_star_big_on);
//            break;
        case 0:ib1.setBackgroundColor(getResources().getColor(R.color.yellow));
        break;
        case 1:ib2.setBackgroundColor(getResources().getColor(R.color.yellow));
            break;
        case 2:ib3.setBackgroundColor(getResources().getColor(R.color.yellow));
            break;
        case 3:ib4.setBackgroundColor(getResources().getColor(R.color.yellow));
            break;
    }
        vp1.setCurrentItem(i);
    }

    View.OnClickListener onClickListener =new View.OnClickListener() {
        @Override
        public void onClick(View view) {
//先将四个按钮都设成未选图片
            resetImage();           ///重新设置图片
            switch (view.getId())
            {
                case R.id.ib1:
                    selectTab(0); break;
                case R.id.ib2:
                    selectTab(1); break;

                case R.id.ib3:
                    selectTab(2); break;

                case R.id.ib4:
                    selectTab(3); break;
            }
        }
    };
    private void initEvents() {
        ib1.setOnClickListener(onClickListener);
        ib2.setOnClickListener(onClickListener);
        ib3.setOnClickListener(onClickListener);
        ib4.setOnClickListener(onClickListener);
    }
    private void initFragment() {
        listfragment=new ArrayList<Fragment>();
        listfragment.add(new HomeFragment());
        listfragment.add(new CollectFragment());
        listfragment.add(new SearchFragment());
        listfragment.add(new MeFragment());
        fpadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) { return listfragment.get(position);
            }
            @Override
            public int getCount() { return listfragment.size();}
        };
        vp1.setAdapter(fpadapter);
//        vp1.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int
//                    positionOffsetPixels) {}
//            @Override
//            public void onPageSelected(int position) {
//                vp1.setCurrentItem(position);
//                resetImage();
//                selectTab(position);
//            }
//            @Override
//            public void onPageScrollStateChanged(int state) {}
//        });
    }



}
